﻿package com.gengine.modules
{
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.system.*;

    public class ModuleLoader extends EventDispatcher
    {
        private var _url:String;
        private var _loader:Loader;
        private var _contentType:String;
        private var _loaderInfo:LoaderInfo;
        protected var _appDomain:ApplicationDomain;

        public function ModuleLoader()
        {
            return;
        }// end function

        public function load(param1:String, param2:LoaderContext = null) : void
        {
            if (this._url != param1)
            {
                this._url = param1;
                if (this._loader == null)
                {
                    this._loader = new Loader();
                }
                else
                {
                    this._loader.contentLoaderInfo.removeEventListener(Event.INIT, this.onInitHandler);
                    this._loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, this.onCompleteHandler);
                    this._loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, this.onProgressHandler);
                    this._loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, this.onIOErrorHandler);
                }
                this._loader.contentLoaderInfo.addEventListener(Event.INIT, this.onInitHandler);
                this._loader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.onCompleteHandler);
                this._loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, this.onProgressHandler);
                this._loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, this.onIOErrorHandler);
                this._loader.load(new URLRequest(this._url), param2);
            }
            return;
        }// end function

        private function onInitHandler(event:Event) : void
        {
            this._loaderInfo = this._loader.contentLoaderInfo;
            this._appDomain = this._loaderInfo.applicationDomain;
            return;
        }// end function

        private function onCompleteHandler(event:Event) : void
        {
            this._loader.contentLoaderInfo.removeEventListener(Event.INIT, this.onInitHandler);
            this._loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, this.onCompleteHandler);
            this._loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, this.onProgressHandler);
            this._loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, this.onIOErrorHandler);
            dispatchEvent(event);
            return;
        }// end function

        private function onProgressHandler(event:ProgressEvent) : void
        {
            dispatchEvent(event);
            return;
        }// end function

        private function onIOErrorHandler(event:IOErrorEvent) : void
        {
            dispatchEvent(event);
            return;
        }// end function

        public function dispose() : void
        {
            this._loader.contentLoaderInfo.removeEventListener(Event.INIT, this.onInitHandler);
            this._loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, this.onCompleteHandler);
            this._loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, this.onProgressHandler);
            this._loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, this.onIOErrorHandler);
            this._loader.unload();
            this._loader = null;
            this._url = null;
            return;
        }// end function

        public function get url() : String
        {
            return this._url;
        }// end function

        public function get content() : DisplayObject
        {
            if (this._loaderInfo)
            {
                return this._loaderInfo.content;
            }
            return null;
        }// end function

        public function get contentType() : String
        {
            if (this._loaderInfo)
            {
                return this._loaderInfo.contentType;
            }
            return null;
        }// end function

        public function get loaderInfo() : LoaderInfo
        {
            return this._loaderInfo;
        }// end function

        public function getExportedAsset(param1:String) : Object
        {
            if (this._appDomain == null)
            {
                throw new Error("not initialized");
            }
            var _loc_2:* = this.getAssetClass(param1);
            if (_loc_2 != null)
            {
                return new _loc_2;
            }
            return null;
        }// end function

        public function getAssetClass(param1:String) : Class
        {
            if (this._appDomain == null)
            {
                throw new Error("not initialized");
            }
            if (this._appDomain.hasDefinition(param1))
            {
                return this._appDomain.getDefinition(param1) as Class;
            }
            return null;
        }// end function

    }
}
